package com.fifth.apartment.service.impl;

import com.fifth.apartment.service.MailService;
import com.fifth.apartment.vo.MailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * TODO
 *
 * @author Dao
 * @since 2024/7/15
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public Boolean sendMail(MailVO vo) {
        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(vo.getFrom()+"(庄泽)");
        message.setTo(vo.getTo());
        message.setSubject(vo.getSubject());
        message.setText(vo.getContent());

       javaMailSender.send(message);
       log.info("邮件发送完毕");
        return true;
    }

    @Override
    public Boolean sendHTMLMail(MailVO vo) {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom(vo.getFrom()+"(庄泽)");
            helper.setTo(vo.getTo());
            helper.setSubject(vo.getSubject());
            helper.setText(vo.getContent(),true);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
      javaMailSender.send(message);
        log.info("携带网页数据进行邮件发送成功");
        return true;
    }

    @Override
    public Boolean sendAttchMail(MailVO vo) {
        MimeMessage message = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message,true);
            helper.setFrom(vo.getFrom()+"(庄泽)");
            helper.setTo(vo.getTo());
            helper.setSubject(vo.getSubject());
            helper.setText(vo.getContent());

            //携带附件
            //D:/FifthProject/apartment_parent/apartment_backend/src/main/resources/image/1.png
            File file1 = ResourceUtils.getFile("classpath:image/a.png");
            File file2 = ResourceUtils.getFile("classpath:image/b.png");
            File file3 = ResourceUtils.getFile("classpath:image/c.png");
            //File file = ResourceUtils.getFile(vo.getAttch());
            helper.addAttachment("图片a.png",file1);
            helper.addAttachment("图片b.png",file2);
            helper.addAttachment("图片c.png",file3);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

        javaMailSender.send(message);
        log.info("携带附件进行邮件发送成功");
        return true;
    }
}
